package NBU::StorageUnit;

require 5.002;

use strict;
use Carp;

BEGIN {
  use Exporter   ();
  use AutoLoader qw(AUTOLOAD);
  use vars       qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD);
  $VERSION =     '0.01';
  @ISA =         qw();
  @EXPORT =      qw();
  @EXPORT_OK =   qw();
  %EXPORT_TAGS = qw();
}

my %stuList;

my %mediaTypes = (
  2 => "Media Manager",
);
sub new {
  my $Class = shift;
  my $stu = {};

  bless $stu, $Class;

  if (@_) {
    $stu->{LABEL} = shift;
    my $type;
    if (exists($mediaTypes{$type = shift})) {
      $stu->{MEDIATYPE} = $mediaTypes{$type};
    }
    else {
      $stu->{MEDIATYPE} = $type;
    }
  }

  $stuList{$stu->{LABEL}} = $stu;
  return $stu;
}

sub populate {
  my $Class = shift;

  open(PIPE, $NBU::prefix."netbackup/bin/admincmd/bpstulist |");
  while (<PIPE>) {
    my ($label, $mediaType, $host,
	$robotType, $robotNumber, $density,
	$numberOfDrives,
	$maxFragmentSize, , $onDemandOnly,
	$maxMPXperDrive) = split;

    my $stu = NBU::StorageUnit->new($label, $mediaType);

    if (!defined(my $robot = NBU::Robot->byID($robotNumber))) {
      $robot = NBU::Robot->new($robotNumber, $robotType, $host);
      $stu->{ROBOT} = $robot;
    }

    $stu->{DRIVECOUNT} = $numberOfDrives;
  }
  close(PIPE);
}

sub list {
  my $Class = shift;

  return (values %stuList);
}

sub byLabel {
  my $Class = shift;
  my $label = shift;

  return $stuList{$label};
}

sub mediaType {
  my $self = shift;

  return $self->{MEDIATYPE};
}

sub driveCount {
  my $self = shift;

  return $self->{DRIVECOUNT};
}

sub label {
  my $self = shift;

  return $self->{LABEL};
}

sub robot {
  my $self = shift;

  return $self->{ROBOT};
}

1;
__END__
